package com.musollo.domain;

import java.io.File;

import org.apache.commons.lang.StringUtils;

/**
 * This class is a representation of an MP3 file. To retreiven an instance of
 * this class, use a TagReader implementation, to update the actual information
 * to the harddrive, use a TagWriter implementation.
 * 
 * @author paw.janssen
 */
public class MP3 {
	private String album;

	private String artist;
	private final File mp3File;

	private String genre, title, comment;

	private final int length;

	private int albumTrackNumber;
	private String date;

	private String puid;

	/**
	 * Initializes the MP3 class and starts the parsing process.
	 * 
	 * @param mp3File
	 *            - The file on the harddrive representing the actual MP3
	 * @param length
	 *            - The length (duration) of the MP3 in seconds
	 */
	public MP3(final File mp3File, final int length) {
		this.mp3File = mp3File;
		this.length = length;
	}

	/**
	 * Two objects of this type are equal if the artist and title information is
	 * the same (ignoring the case of the two strings).
	 * 
	 * @param o
	 *            The object to compare to.
	 * 
	 * @return true, if equals
	 */
	@Override
	public boolean equals(final Object o) {
		if (o instanceof MP3) {
			MP3 mp3 = (MP3) o;
			if (mp3.getTitle().equalsIgnoreCase(this.getTitle())) {
				if (mp3.getArtist().equalsIgnoreCase(this.getArtist())) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Gets the album.
	 * 
	 * @return - The album information of the MP3, or null if no album
	 *         information is present.
	 */
	public String getAlbum() {
		return album;
	}

	/**
	 * Gets the album track number.
	 * 
	 * @return - The track number of the mp3 on the album or 0 if no track
	 *         number information was present.
	 * 
	 */
	public int getAlbumTrackNumber() {
		return albumTrackNumber;
	}

	/**
	 * Gets the artist.
	 * 
	 * @return - The artist information of the MP3, or null if no artist
	 *         information is present.
	 */
	public String getArtist() {
		return artist;
	}

	/**
	 * Gets the comment.
	 * 
	 * @return - The comment information of the MP3, or null if no comment
	 *         information is present.
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * Gets the year.
	 * 
	 * @return - The year information of the MP3, or null if no year information
	 *         is present.
	 */
	public String getDate() {
		return date;
	}

	/**
	 * Gets the genre.
	 * 
	 * @return - The genre information of the MP3, or null if no genre
	 *         information is present.
	 */
	public String getGenre() {
		return genre;
	}

	/**
	 * Gets the length.
	 * 
	 * @return - The song length in seconds.
	 */
	public int getLength() {
		return length;
	}

	/**
	 * Gets the file name.
	 * 
	 * @return - The fileName (including full path to) the actual MP3 file.
	 */
	public File getMp3File() {
		return this.mp3File;
	}

	public String getPuid() {
		return puid;
	}

	/**
	 * Gets the title.
	 * 
	 * @return - The title information of the MP3, or null if no title
	 *         information is present.
	 */
	public String getTitle() {
		return title;
	}

	@Override
	public int hashCode() {
		int hashCode = this.getArtist().toLowerCase().hashCode() + this.getTitle().toLowerCase().hashCode();
		return hashCode;
	}

	public void setAlbum(final String album) {
		if (StringUtils.isBlank(album)) {
			this.album = null;
		} else {
			this.album = album;
		}
	}

	public void setAlbumTrackNumber(final int albumTrackNumber) {
		this.albumTrackNumber = albumTrackNumber;
	}

	public void setArtist(final String artist) {
		if (StringUtils.isBlank(artist)) {
			this.artist = null;
		} else {
			this.artist = artist;
		}
	}

	public void setComment(final String comment) {
		if (StringUtils.isBlank(comment)) {
			this.comment = null;
		} else {
			this.comment = comment;
		}
	}

	public void setDate(final String date) {
		if (StringUtils.isBlank(title)) {
			this.date = null;
		} else {
			this.date = date;
		}
	}

	public void setGenre(final String genre) {
		if (StringUtils.isBlank(genre)) {
			this.genre = null;
		} else {
			this.genre = genre;
		}
	}

	public void setPuid(final String puid) {
		if (StringUtils.isBlank(puid)) {
			this.puid = null;
		} else {
			this.puid = puid;
		}
	}

	public void setTitle(final String title) {
		if (StringUtils.isBlank(title)) {
			this.title = null;
		} else {
			this.title = title;
		}
	}

	@Override
	public String toString() {
		String mp3Info;
		mp3Info = "Filename: " + this.mp3File + "\n";
		mp3Info += "Artist: " + this.artist + "\n";
		mp3Info += "Title: " + this.title + "\n";
		mp3Info += "Album: " + this.album + "\n";
		mp3Info += "Genre: " + this.genre + "\n";
		mp3Info += "Year: " + this.date + "\n";
		mp3Info += "Length: " + this.length + "\n";
		mp3Info += "Comment: " + this.comment + "\n";
		mp3Info += "Tracknumber : " + this.albumTrackNumber + "\n";
		return mp3Info;
	}

}
